<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
    @open="openDialog"
  >
    <el-tree
      ref="tree"
      :data="permissionData"
      default-expand-all
      show-checkbox
      :props="defaultProps"
      node-key="id"
    />
    <!-- 权限点数据展示 -->
    <template #footer>
      <div style="text-align: right;">
        <el-button @click="closeAssignDialog">取消</el-button>
        <el-button type="primary" @click="assignPerm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { assignPerm, getRoleDetail } from '@/api/setting'
export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    },
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionData: [], // 存储权限数据
      defaultProps: {
        label: 'name'
      }
    }
  },
  methods: {
    closeAssignDialog() {
      this.$emit('close-dialog-assign')
      this.$refs.tree.setCheckedKeys([])
    },
    async assignPerm() {
      await assignPerm({ id: this.roleId, permIds: this.$refs.tree.getCheckedKeys() })
      this.$message.success('分配权限成功!')
      this.closeAssignDialog()
    },
    async openDialog() {
      const res = await getRoleDetail(this.roleId)
      this.$refs.tree.setCheckedKeys(res.permIds)
    }
  }
}
</script>

<style>

</style>
